use anyhow::{anyhow, Result};
use db::Db;

// use db::models::MatrixOrTelegramId;
use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
use matrix_sdk::{room::Joined, ruma::events::room::message::OriginalSyncRoomMessageEvent, Client};
use std::ops::Deref;
use uuid::Uuid;

pub async fn count_unread_posts(
    ev: OriginalSyncRoomMessageEvent,
    _text: String,
    room: Joined,
    _client: Client,
    db: Db,
) -> Result<()> {
    log::info!("counting unread posts");
    let user_id = ev.sender.to_string();
    // let user_id = MatrixOrTelegramId::MatrixId(&matrix_id);
    // let user_id = db.get_user_id(&user_id).await.unwrap();
    let num_of_posts = db.count_unread_posts_db_matrix(&user_id).await?;
        let content = {
            if num_of_posts.gt(&0) {
                RoomMessageEventContent::text_markdown(format!("Unread posts: {num_of_posts}"))
            } else {
                RoomMessageEventContent::text_markdown("No unread posts!")
            }
        };
        room.send(content, None).await?;
    
    Ok(())
}

